<!--
 * @Description: 
 * @Author: liuzl
 * @Date: 2024-11-15 08:37:13
 * @LastEditors: liuzl
 * @LastEditTime: 2024-11-15 11:01:23
-->
<template>
  <div class="min-h-screen bg-gray-50 py-8 px-4">
    <div class="max-w-6xl mx-auto">
      <!-- Header -->
      <div class="flex justify-between items-center mb-6">
        <el-button type="primary" @click="handleAdd">
          新增
        </el-button>
        <div class="flex gap-2">
          <el-input
            v-model="searchQuery"
            placeholder="请输入搜索内容"
            class="w-64"
          />
          <el-button type="primary">
            查询
          </el-button>
        </div>
      </div>

      <!-- Table -->
      <el-table 
        :data="filteredTableData" 
        border 
        class="w-full"
        :header-cell-style="{
          background: '#f5f7fa',
          color: '#606266',
          fontWeight: 'bold'
        }"
      >
        <el-table-column prop="id" label="学号" width="180" />
        <el-table-column prop="name" label="姓名" width="180" />
        <el-table-column prop="score" label="分数" width="180" />
        <el-table-column prop="desc" label="评语" />
        <el-table-column fixed="right" label="操作" width="200">
          <template #default="scope">
            <el-button 
              type="primary" 
              link
              @click="handleEdit(scope.row)"
            >
              编辑
            </el-button>
            <el-button 
              type="danger" 
              link
              @click="handleDelete(scope.row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- Edit Dialog -->
    <el-dialog 
      v-model="dialogVisible" 
      :title="isEditing ? '编辑学生' : '新增学生'"
      width="500px"
    >
      <el-form 
        :model="form" 
        label-width="80px"
        :rules="rules"
        ref="formRef"
      >
        <el-form-item label="学号" prop="id">
          <el-input 
            v-model="form.id" 
            :disabled="isEditing"
            placeholder="请输入学号"
          />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input 
            v-model="form.name" 
            placeholder="请输入姓名"
          />
        </el-form-item>
        <el-form-item label="分数" prop="score">
          <el-input-number 
            v-model="form.score" 
            :min="0" 
            :max="100"
            placeholder="请输入分数"
          />
        </el-form-item>
        <el-form-item label="评语" prop="desc">
          <el-input 
            v-model="form.desc" 
            type="textarea" 
            placeholder="请输入评语"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="flex justify-end gap-2">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确认</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const tableData = ref([
  {
    id: '001',
    name: '张三',
    score: 90,
    desc: "很优秀"
  },
  {
    id: '002',
    name: '李四',
    score: 80,
    desc: "优秀"
  },
  {
    id: '003',
    name: '王五',
    score: 70,
    desc: "良好"
  }
])

const searchQuery = ref('')
const dialogVisible = ref(false)
const isEditing = ref(false)
const formRef = ref(null)

const form = ref({
  id: '',
  name: '',
  score: 0,
  desc: ''
})

const rules = {
  id: [{ required: true, message: '请输入学号', trigger: 'blur' }],
  name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  score: [{ required: true, message: '请输入分数', trigger: 'blur' }]
}

const filteredTableData = computed(() => {
  if (!searchQuery.value) return tableData.value
  
  return tableData.value.filter(item => 
    item.name.includes(searchQuery.value) ||
    item.id.includes(searchQuery.value) ||
    item.desc.includes(searchQuery.value)
  )
})

function handleAdd() {
  isEditing.value = false
  form.value = {
    id: '',
    name: '',
    score: 0,
    desc: ''
  }
  dialogVisible.value = true
}

function handleEdit(row) {
  isEditing.value = true
  form.value = { ...row }
  dialogVisible.value = true
}

function handleDelete(id) {
  ElMessageBox.confirm(
    '确认删除该学生记录吗？',
    '警告',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    tableData.value = tableData.value.filter(item => item.id !== id)
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
  }).catch(() => {})
}

async function handleSubmit() {
  if (!formRef.value) return
  
  await formRef.value.validate((valid) => {
    if (valid) {
      if (isEditing.value) {
        const index = tableData.value.findIndex(item => item.id === form.value.id)
        if (index !== -1) {
          tableData.value[index] = { ...form.value }
        }
      } else {
        // Check if ID already exists
        if (tableData.value.some(item => item.id === form.value.id)) {
          ElMessage.error('学号已存在')
          return
        }
        tableData.value.push({ ...form.value })
      }
      
      dialogVisible.value = false
      ElMessage({
        type: 'success',
        message: isEditing.value ? '编辑成功' : '添加成功'
      })
    }
  })
}
</script>

<style scoped>
:deep(.el-table) {
  --el-table-border-color: #dcdfe6;
  --el-table-header-bg-color: #f5f7fa;
}

:deep(.el-dialog) {
  border-radius: 8px;
}
</style>